package cn.zn.controller.system;

import cn.zn.base.annotation.Log;
import cn.zn.base.bean.dto.ResultData;
import cn.zn.base.common.enums.BusinessType;
import cn.zn.base.common.utils.ResultUtil;
import cn.zn.framework.utils.SecurityUtil;
import cn.zn.menu.entity.SysMenu;
import cn.zn.user.entity.SysUser;
import cn.zn.menu.service.SysMenuService;
import cn.zn.menu.vo.SysMenuEditVo;
import cn.zn.menu.vo.SysMenuPageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单信息
 *
 * @author zn-admin
 */
@RestController
@RequestMapping("/system/menu")
public class SysMenuController {

    @Autowired
    private SysMenuService menuService;

    /**
     * 获取菜单列表
     */
    @PreAuthorize("@ss.hasPermi('system:menu:list')")
    @GetMapping("/list")
    public ResultData list(SysMenuPageVo vo) {
        SysUser theUser = SecurityUtil.getTheUser();
        List<SysMenu> menus = menuService.selectMenuList(vo, theUser);
        return ResultUtil.success(menus);
    }

    /**
     * 根据菜单编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:menu:info')")
    @GetMapping(value = "/{menuId}")
    public ResultData getInfo(@PathVariable String menuId) {
        return ResultUtil.success(menuService.getById(menuId));
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/treeSelect")
    public ResultData treeSelect(SysMenuPageVo vo) {
        SysUser theUser = SecurityUtil.getTheUser();
        List<SysMenu> menus = menuService.selectMenuList(vo, theUser);
        return ResultUtil.success(menuService.buildMenuTreeSelect(menus));
    }

    /**
     * 新增菜单
     */
    @PreAuthorize("@ss.hasPermi('system:menu:add')")
    @Log(title = "菜单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@RequestBody @Validated SysMenuEditVo vo) {
        menuService.insertMenu(vo);
        return ResultUtil.success();
    }

    /**
     * 修改菜单
     */
    @PreAuthorize("@ss.hasPermi('system:menu:edit')")
    @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/{id}")
    public ResultData edit(@PathVariable String id, @RequestBody @Validated SysMenuEditVo vo) {
        menuService.updateMenu(id, vo);
        return ResultUtil.success();
    }

    /**
     * 删除菜单
     */
    @PreAuthorize("@ss.hasPermi('system:menu:remove')")
    @Log(title = "菜单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{menuId}")
    public ResultData remove(@PathVariable("menuId") String menuId) {
        menuService.deleteMenuById(menuId);
        return ResultUtil.success();
    }
}